package com.example.controller;

import com.example.pojo.Collection;
import com.example.pojo.Comment;
import com.example.pojo.Work;
import com.example.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/work")
@CrossOrigin(origins = "*")
public class WorkController {
    @Autowired
    private WorkService workService;

    // 获取所有作品
    @GetMapping("/all")
    public List<Work> getAllWorks() {
        List<Work> works = workService.findAll();
        works.forEach(work -> {
            System.out.println("Work ID: " + work.getId());
            System.out.println("Image URL length: " + 
                (work.getImgUrl() != null ? work.getImgUrl().length() : 0));
        });
        return works;
    }

    // 发布作品
    @PostMapping("/publish")
    public String publishWork(@RequestBody Work work) {
        try {
            System.out.println("Received work with userId: " + work.getUserId());
            if (work.getUserId() == null) {
                return "发布失败：未登录用户";
            }
            workService.insert(work);
            return "作品发布成功！";
        } catch (Exception e) {
            e.printStackTrace();
            return "发布失败：" + e.getMessage();
        }
    }

    // 评论作品
    @PostMapping("/comment")
    public String commentOnWork(@RequestBody Comment comment) {
        workService.insertComment(comment);
        return "评论成功！";
    }

    //收藏作品
    @PostMapping("/collection")
    public String favoriteWork(@RequestBody Collection collection) {
        try {
            System.out.println("Received work with userId: " + collection.getUserId());
            workService.insertCollection(collection);
            return "收藏成功！";
        } catch (Exception e) {
            e.printStackTrace();
            return "收藏失败：" + e.getMessage();
        }
    }
    //查看收藏作品
    @GetMapping("/collection/{userId}")
    public List<Collection> getUserCollections(@PathVariable Integer userId) {
        try {
            return workService.getUserCollections(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return Collections.emptyList(); // 返回空列表表示失败
        }
    }
    //检查是否收藏
    @GetMapping("/collection/check/{userId}/{workId}")
    public boolean checkCollection(@PathVariable Integer userId, @PathVariable Integer workId) {
        return workService.checkCollection(userId, workId);
    }
    //取消收藏
    @DeleteMapping("/collection/{userId}/{workId}")
    public String cancelCollection(@PathVariable Integer userId, @PathVariable Integer workId) {
        try {
            workService.deleteCollection(userId, workId);
            return "取消收藏成功！";
        } catch (Exception e) {
            e.printStackTrace();
            return "取消收藏失败：" + e.getMessage();
        }
    }

    // 更新作品浏览量
    @PutMapping("/views/{id}")
    public String incrementViews(@PathVariable Integer id) {
        workService.incrementViews(id);
        return "浏览量增加成功！";
    }

    // 更新作品点赞数
    @PutMapping("/likes/{id}")
    public String incrementLikes(@PathVariable Integer id) {
        workService.incrementLikes(id);
        return "点赞成功！";
    }

    // 获取某个作品的评论
    @GetMapping("/{workId}/comments")
    public List<Comment> getComments(@PathVariable Integer workId) {
        return workService.findCommentsByWorksId(workId);
    }

    //获取某个作品的详细信息
    @GetMapping("/{id}")
    public Work getWorkById(@PathVariable Integer id) {
        return workService.findById(id);
    }

    // 获取指定用户的所有作品
    @GetMapping("/user/{userId}")
    public List<Work> getWorksByUserId(@PathVariable Integer userId) {
        return workService.findByUserId(userId);
    }

    //删除作品
    @DeleteMapping("/{id}")
    public String deleteWork(@PathVariable Integer id) {
        workService.deleteById(id);
        return "删除成功！";
    }
}
